package com.sx.db.sharding.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sx.db.sharding.entity.SxOperate;
import com.sx.db.sharding.entity.SxUser;
import com.sx.db.sharding.mapper.SxOperateMapper;
import com.sx.db.sharding.mapper.SxUserMapper;
import com.sx.db.sharding.service.SxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 用户Service实现类
 */
@Service
public class SxUserServiceImpl extends ServiceImpl<SxUserMapper, SxUser> implements SxUserService {

    @Autowired
    private SxUserMapper sxUserMapper;

    @Autowired
    private SxOperateMapper sxOperateMapper;

    @Override
    public SxUser getById(Long id) {
        return sxUserMapper.selectById(id);
    }

    @Override
    public boolean saveUser(SxUser sxUser) {
        sxUser.setCreateTime(LocalDateTime.now());
        sxUser.setUpdateTime(LocalDateTime.now());
        sxUser.setStatus(1); // 默认启用
        boolean result = sxUserMapper.insert(sxUser) > 0;

        // 记录操作日志
        if (result) {
            SxOperate operate = new SxOperate();
            operate.setUserId(sxUser.getId());
            operate.setOperateType("INSERT");
            operate.setTableName("sx_user");
            operate.setContent("新增用户: " + sxUser.getUsername());
            operate.setIp("127.0.0.1"); // 实际应用中获取真实IP
            operate.setOperateTime(LocalDateTime.now());
            operate.setOperator(sxUser.getUsername());
            sxOperateMapper.insert(operate);
        }

        return result;
    }

    @Override
    public boolean updateUser(SxUser sxUser) {
        sxUser.setUpdateTime(LocalDateTime.now());
        boolean result = sxUserMapper.updateById(sxUser) > 0;

        // 记录操作日志
        if (result) {
            SxOperate operate = new SxOperate();
            operate.setUserId(sxUser.getId());
            operate.setOperateType("UPDATE");
            operate.setTableName("sx_user");
            operate.setContent("更新用户: " + sxUser.getUsername());
            operate.setIp("127.0.0.1");
            operate.setOperateTime(LocalDateTime.now());
            operate.setOperator(sxUser.getUsername());
            sxOperateMapper.insert(operate);
        }

        return result;
    }

    @Override
    public boolean removeById(Long id) {
        SxUser user = sxUserMapper.selectById(id);
        if (Objects.isNull(user)) {
            return false;
        }

        boolean result = sxUserMapper.deleteById(id) > 0;

        // 记录操作日志
        if (result) {
            SxOperate operate = new SxOperate();
            operate.setUserId(id);
            operate.setOperateType("DELETE");
            operate.setTableName("sx_user");
            operate.setContent("删除用户: " + user.getUsername());
            operate.setIp("127.0.0.1");
            operate.setOperateTime(LocalDateTime.now());
            operate.setOperator("system");
            sxOperateMapper.insert(operate);
        }

        return result;
    }
}